package com.hugoo.rpt.slt;

import com.hugoo.core.Conf;
import com.hugoo.core.db.DBC;
import com.hugoo.core.slt.AcFwd;
import com.hugoo.core.slt.Action;
import com.hugoo.core.util.TL;
import com.hugoo.rpt.ExpPageInfo;
import com.hugoo.rpt.TextPageInfo;
import com.hugoo.rpt.busz.HLinkHtmlGenAction;
import com.hugoo.rpt.busz.HtmlGenAction;
import com.hugoo.rpt.busz.TxtExpAction;
import com.hugoo.rpt.busz.XlsExpAction;
import com.hugoo.rpt.busz.XlstExpAction;
import com.hugoo.rpt.busz.XlsxExpAction;
import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Hugoo.Chan
 */
public class RptAction extends Action {

    /**
     * 查询报表数据 本处现设置一种特殊情况：
     * 如果在传入的参数中包括【_imp_】则以_imp_的值来定加载什么样的数据报表
     * 1、【DB】加载数据库自动配置类型报表 2、【FILE】加载文件配置类型报表
     *
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public AcFwd query(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (super.checkPara(request, response)) {
            TextPageInfo pi;
            try {
                pi = new HtmlGenAction(_sid, request);
                new DBC().loadPageInfo(pi);
                writeDataToClient(response, pi.getTextInfo());
            } catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }
        return null;
    }

    public AcFwd hlink(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (super.checkPara(request, response)) {
            try {
                TextPageInfo pi = new HLinkHtmlGenAction(_sid, request);
                new DBC().loadPageInfo(pi);
                writeDataToClient(response, pi.getTextInfo());
            } catch (Exception e) {
                e.printStackTrace(System.err);
            }
        }
        return null;
    }

    public AcFwd load(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String tpl = null;
        if (super.checkPara(request, response)) {
            try {
                tpl = Conf.getAutqProp(_sid, Conf.RPT._rpt_htm_tpl);
                request.setAttribute("_sid", _sid);
                Calendar c = Calendar.getInstance();
                c.add(Calendar.MONTH, -1);
                request.setAttribute("_defaultMonth", TL.format(c.getTime(), "yyyy-MM"));
                request.setAttribute("_location", Conf.getAutqProp(_sid, Conf.RPT._rpt_location));
            } catch (Exception e) {
                e.printStackTrace(System.err);
            }
            return new AcFwd(tpl);
        }
        return null;
    }

    public AcFwd txt(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (super.checkPara(request, response)) {
            ExpPageInfo pi = new TxtExpAction(_sid, request);
            new DBC().loadPageInfo(pi);
            writeDataToClient(request, response, pi.getFile(), pi.getFileName());
        }
        return null;

    }

    public AcFwd xls(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (super.checkPara(request, response)) {
            ExpPageInfo pi = new XlsExpAction(_sid, request);
            new DBC().loadPageInfo(pi);
            writeDataToClient(request, response, pi.getFile(), pi.getFileName());
        }
        return null;
    }

    /**
     * Excel 2003格式 可以根据指定的模板文件进行数据导出
     *
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public AcFwd xlst(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (super.checkPara(request, response)) {
            String st = Conf.getAutqProp2(_sid, Conf.RPT._rpt_xls_tpl);//
            ExpPageInfo pi;
            if (TL.isEmpty(st)) {
                pi = new XlsExpAction(_sid, request);
            } else {
                pi = new XlstExpAction(_sid, request);
            }
            new DBC().loadPageInfo(pi);
            writeDataToClient(request, response, pi.getFile(), pi.getFileName());
        }
        return null;
    }

    /**
     * Excel 2007格式导出， 本功能可以导出超大数据集
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public AcFwd xlsx(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (super.checkPara(request, response)) {
            ExpPageInfo pi = new XlsxExpAction(_sid, request);
            new DBC().loadPageInfo(pi);
            writeDataToClient(request, response, pi.getFile(), pi.getFileName());
        }
        return null;
    }

    public AcFwd xlsxt(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        if (super.checkPara(request, response)) {
            ExpPageInfo pi = new XlsxExpAction(_sid, request);
            new DBC().loadPageInfo(pi);
            writeDataToClient(request, response, pi.getFile(), pi.getFileName());
        }
        return null;
    }
}
